package junit.test_1;

import static org.junit.Assert.*;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.junit.BeforeClass;
import org.junit.Test;

import bean.Person;
import bean.Product;
import utils.DateTool;

public class VelocityTest01 {
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception{
		//Velocity.init("src/velocity.properties");
		Properties properties = new Properties();
		properties.put("runtime log" ,"F:\\newworkspace\\velocity_new\\log\\velocity.log");
		properties.put("file.resource.loader.path" ,"F:\\newworkspace\\velocity_new\\vm");
		properties.put("input.enconing" ,"UTF-8");
		properties.put("output.encoding" ,"UTF-8");
		Velocity.init(properties);
	}
	
	@Test
	public void helloWorld(){
		try{
			VelocityContext context = new VelocityContext();
			context.put("helloWorld","你好，帮学试");
			Template template = Velocity.getTemplate("helloWorld.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	
	@Test
	public void loopList(){
		try{
			VelocityContext context = new VelocityContext();
			context.put("persons",new Person[]{new Person(9527,"周星驰"),new Person(110,"成龙")});
			Template template = Velocity.getTemplate("persons.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void loopMap(){
		try{
			Map<String,String> maps = new HashMap<String,String>();
			maps.put("坂田银时", "银魂");
			maps.put("漩涡鸣人", "火影忍者");
			VelocityContext context = new VelocityContext();
			context.put("maps",maps);
			Template template = Velocity.getTemplate("maps.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void pageSet(){
		try{
			VelocityContext context = new VelocityContext();
			Template template = Velocity.getTemplate("pageset.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void ifara(){
		try{
			VelocityContext context = new VelocityContext();
			context.put("bool", false);
			Template template = Velocity.getTemplate("condition.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void fomatDate(){
		try{			
			VelocityContext context = new VelocityContext();
			context.put("now", new Date());
			context.put("dateTool", new DateTool());
			Template template = Velocity.getTemplate("date.vm");
			StringWriter writer = new StringWriter();
			template.merge(context, writer);
			System.out.println(writer.toString());
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
	
	@Test
	public void outFile(){
		try{
			Product product = new Product(10,"瑜伽球",null);			
			VelocityContext context = new VelocityContext();
			context.put("product",product);
			Template template = Velocity.getTemplate("productTemplate.html");
			File saveFile = new File("html/product.html");
			if(!saveFile.getParentFile().exists()){
				saveFile.getParentFile().mkdirs();
			}
			FileOutputStream outStream = new FileOutputStream(saveFile);
			OutputStreamWriter outStreamWriter =new OutputStreamWriter(outStream,"UTF-8");
			BufferedWriter writer = new BufferedWriter(outStreamWriter);
			template.merge(context, writer);
			writer.flush();
			writer.close();
		}catch(Exception e){
			e.printStackTrace();
		}		
	}
}
